package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.service.admin.MarketAdService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;


@RequestMapping("/admin/ad")
@RestController
public class AdminAdvertisingController  {
    @Autowired
     MarketAdService adService ;

    @RequiresPermissions("admin:ad:update")
    @RequiresPermissionsDesc(menu = {"广告管理", "广告更新"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody MarketAd marketAd) {
        try{
            int flag = adService.update(marketAd);
            if(flag !=0)
                return ResponseUtil.ok();
            else return ResponseUtil.fail(405,"create failed");
        }catch (Exception e){
            System.out.println("e = " + e);
            return ResponseUtil.fail(405,"update failed");
        }
    }
    @RequiresPermissions("admin:ad:create")
    @RequiresPermissionsDesc(menu = {"广告管理", "广告创建"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody MarketAd marketAd) throws IOException {
        try {
            int flag = adService.create(marketAd);
            if(flag !=0)
            return ResponseUtil.ok();
            else return ResponseUtil.fail(405,"create failed");
        }catch (Exception e){
            System.out.println("e = " + e);
            return ResponseUtil.fail(405,"create failed");
        }
    }
    @RequiresPermissions("admin:ad:list")
    @RequiresPermissionsDesc(menu = {"广告管理", "广告列表"}, button = "显示")
    @GetMapping("/list")
    public Object list(String page,String limit,String sort,String order,String name, String content){

        int pageInt = 0;
        int limitInt = 0;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        }catch (NumberFormatException e) {
            ResponseUtil.fail(404,"page和limit不能为非正整数");
        }
        try {
            List<MarketAd> marketAdList = adService.list(pageInt, limitInt, sort, order, name, content);
            return ResponseUtil.okList(marketAdList);
        }catch (Exception e) {
            System.out.println("e = " + e);
            return ResponseUtil.fail(405,"list failed");
        }
    }
    @RequiresPermissions("admin:ad:delete")
    @RequiresPermissionsDesc(menu = {"广告管理", "广告删除"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody MarketAd marketAd) throws IOException {
        try {
            int flag = adService.delete(marketAd.getId());
            if(flag !=0)
                return ResponseUtil.ok();
            else return ResponseUtil.fail(405,"create failed");
        }catch (Exception e) {
            System.out.println("e = " + e);
            return ResponseUtil.fail(405,"delete failed");
        }
    }
}
